package com.zichong.wiki.controller;

import com.zichong.wiki.req.CategoryQueryReq;
import com.zichong.wiki.req.CategorySaveReq;
import com.zichong.wiki.resp.CommonResp;
import com.zichong.wiki.resp.CategoryQueryResp;
import com.zichong.wiki.resp.PageResp;
import com.zichong.wiki.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/all")
    public CommonResp all(@Valid CategoryQueryReq req) {
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        List<CategoryQueryResp> list = categoryService.all(req);
        resp.setContent(list);
        return resp;
    }

    /**
     * params e.g.:
     * /category/list
     * /category/list?page=1&size=3
     */
    @GetMapping("/list")
    public CommonResp list(@Valid CategoryQueryReq req) {
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        PageResp<CategoryQueryResp> list = categoryService.list(req);
        resp.setContent(list);

        return resp;
    }

    /**
     * params e.g.:
     * create:
     * {
     *     "parent": 222,
     *     "name": "111",
     *     "sort": 333
     * }
     *
     * update:
     * {
     *     "id": 135341531061161984,
     *     "parent": 222,
     *     "name": "111",
     *     "sort": 333
     * }
     */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody CategorySaveReq req) {
        CommonResp resp = new CommonResp<>();
        categoryService.save(req);

        return resp;
    }

    /**
     * params e.g.:
     * /category/delete/135341531061161984
     */
    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        categoryService.delete(id);

        return resp;
    }

}
